<?php
/**
  * 一元包邮处理队列文件
  */

define('IN_ECS', true);
define('INIT_NO_USERS', true);
define('INIT_NO_SMARTY', true);
require(dirname(__FILE__) . '/../../includes/init.php');
include_once(ROOT_PATH . 'includes/funBuyone.php');
@ini_set('display_errors',        1);

$buyoneList = getBuyoneList();
$start_time = strtotime('2015-09-18 22:00:00');
$end_time = strtotime('2015-09-19 22:00:00');

if (!empty($_GET['act']) && $_GET['act'] == 'initialization') {
    ini_set("session.cookie_domain", ".eggms.com");
    ini_set("session.gc_maxlifetime", "86400");
    session_start();
    define('SESS_ID', session_id());
    if ($_SESSION['user_id'] != 1 || time() > $start_time) exit('错误404');
    foreach ($buyoneList as $key => $value) {
        $id = $value['parent_id'];
        C()->mem->set("buyoneStock_{$id}", "{$value['stock']}", 86400*5);
    }
    Q()->mem->set("buyoneQueue", '0||', 86400*5);
    Q()->mem->set("buyoneQueue2", '0||', 86400*5);
    Q()->mem->set("buyoneBaoyouQueue", '0||', 86400*5);
    Q()->mem->set("buyoneBaoyouQueue2", '0||', 86400*5);
    M()->set("sumBuyoneUserNum", '3759', 86400*5);
    M()->set("sumBuyoneUserNum2", '0', 86400*5);
    M()->set("setSumBuyone", '400', 86400*5);
    exit;
}




/* 释放包邮卷 */
if (!empty($_GET['act']) && $_GET['act'] == 'baoyoujuan') {
    // 获取要发放的用户
    $queue = array_unique(Q()->get("buyoneBaoyouQueue"));
    // 获取已发放的用户
    $queue2 = array_unique(Q()->get("buyoneBaoyouQueue2"));

    // 比较得出未发放的用户
    $queueBy = array_diff($queue, $queue2);

    /* 包邮卷信息 */
    $baoyoujuan['coupon_code']    = 'xViAqQqG';
    $baoyoujuan['coupon_name']    = '一元包邮专用包邮卷';
    $baoyoujuan['type_id']        = '1';
    $baoyoujuan['is_exchange']    = '1';
    $baoyoujuan['exchange_time']  = time();
    $baoyoujuan['add_time']       = $start_time;
    $baoyoujuan['get_start_time'] = $baoyoujuan['start_time'] = $start_time;
    $baoyoujuan['get_end_time']   = $baoyoujuan['end_time'] = $end_time;

    /* 插入包邮卷 */
    if (!empty($queueBy)) {
        $sql = "INSERT INTO egg_coupon (coupon_code, coupon_name, type_id, add_time, get_start_time, get_end_time, start_time, end_time, user_id, is_exchange, exchange_time) VALUES ";
        /* 生成插入语句的值 */
        foreach ($queueBy as $key => $value) {
            $sqlVal[] = "('{$baoyoujuan['coupon_code']}', '{$baoyoujuan['coupon_name']}', '{$baoyoujuan['type_id']}', '{$baoyoujuan['add_time']}', '{$baoyoujuan['get_start_time']}', '{$baoyoujuan['get_end_time']}', '{$baoyoujuan['start_time']}', '{$baoyoujuan['end_time']}', '{$value}', '{$baoyoujuan['is_exchange']}', '{$baoyoujuan['exchange_time']}') ";
        }
        $sql .= implode(',', $sqlVal);
        $db->query($sql);
        /* 记录已发放用户 */
        foreach ($queueBy as $key => $value) {
            Q()->append("buyoneBaoyouQueue2", $value);
        }
    }
    echo "<pre>包邮卷发放：";
    print_r($queueBy);
    echo "</pre>";

/* 释放一元包邮商品 */
    // 获取要发放的用户
    $queue3 = array_unique(Q()->get("buyoneQueue"));
    // 获取已发放的用户
    $queue4 = array_unique(Q()->get("buyoneQueue2"));
    // 比较得出未发放的用户
    $queueBy2 = array_diff($queue3, $queue4);
    /* 购物车商品信息 */
    $cartInfo['user_id']        = '';
    $cartInfo['goods_price']    = '1.00';
    $cartInfo['market_price']   = '14.90';
    $cartInfo['goods_number']   = '1';
    $cartInfo['is_real']        = '1';
    $cartInfo['extension_code'] = 'buyone';
    $cartInfo['add_time']       = time();
    $cartInfo['is_shipping']    = '1';

    /* 将商品插入购物车 */
    if (!empty($queueBy2)) {
        $sql = "INSERT INTO egg_cart (user_id, parent_id, goods_name, goods_price, market_price, goods_number, is_real, extension_code, extension_id, add_time, is_shipping) VALUES ";
        /* 生成插入语句的值 */
        foreach ($queueBy2 as $key => $value) {
            $var = explode(':', $value);
            $cartInfo['parent_id']      = $buyoneList[$var[1]]['parent_id'];
            $cartInfo['goods_name']     = $buyoneList[$var[1]]['name'];
            $cartInfo['market_price']   = $buyoneList[$var[1]]['original_price'];
            $cartInfo['extension_id']   = $buyoneList[$var[1]]['parent_id'];
            $sqlVal2[] = "('{$var[0]}','{$cartInfo['parent_id']}','{$cartInfo['goods_name']}','{$cartInfo['goods_price']}','{$cartInfo['market_price']}','{$cartInfo['goods_number']}','{$cartInfo['is_real']}','{$cartInfo['extension_code']}','{$cartInfo['extension_id']}','{$cartInfo['add_time']}','{$cartInfo['is_shipping']}') ";
        }
        $sql .= implode(',', $sqlVal2);
        $db->query($sql);
        /* 记录已发放用户 */
        foreach ($queueBy2 as $key => $value) {
            Q()->append("buyoneQueue2", $value);
        }
    }
    echo "<pre>商品发放：";
    print_r($queueBy2);
    echo "</pre>";
    exit;
}
